<!DOCTYPE html>
<html lang="zh-CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>课程列表</title>
    <div th:replace="common/admin_commonElement :: css"></div>
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }

        .tree-closed {
            height: 40px;
        }

        .tree-expanded {
            height: auto;
        }
    </style>
</head>

<body>
<div th:replace="common/admin_commonElement :: nav"></div>
<div class="container-fluid">
    <div class="row">
        <div th:replace="common/admin_commonElement :: sider"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 课程列表</h3>
                </div>
                <form class="form-inline" role="form" style="float:left;">
                    <div class="form-group has-feedback">
                        <div class="input-group">
                            <div class="input-group-addon">查询条件</div>
                            <input id="nameInput" class="form-control has-success" type="text" name="name"
                                   placeholder="请输入查询的课程名称" th:value="${name}">
                        </div>
                    </div>
                    <input id="selectByNameBtn" class="btn btn-warning btn btn-warning glyphicon glyphicon-search"
                           type="button" value="查询">
                </form>
                <button id="addCourse" type="button" class="btn btn-primary" style="float:right;">
                    <i class="glyphicon glyphicon-plus"></i> 添加课程
                </button>
                <br>
                <hr style="clear:both;">
                <div class="table-responsive">
                    <table class="table  table-bordered">
                        <thead>
                        <tr>
                            <th>课程编号</th>
                            <th>课程名称</th>
                            <th>授课教师</th>
                            <th>授课班级</th>
                            <th width="100">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr th:each="course : ${pageQueryUtils.list}">
                            <td th:text="${course.id}">1906Z12143</td>
                            <td th:text="${course.name}">贾佚然</td>
                            <td th:text="${course.teacher.name}">贾佚然</td>
                            <td th:text="${course.grade.name}">贾佚然</td>
                            <td>
                                <button onclick="" type="button" class="btn btn-primary btn-xs"
                                        th:onclick="editCourse([[${course.id}]])">
                                    <i class=" glyphicon glyphicon-pencil"></i>
                                </button>
                                <button type="button" class="btn btn-danger btn-xs"
                                        th:onclick="deleteCourse([[${course.id}]])">
                                    <i class=" glyphicon glyphicon-remove"></i>
                                </button>
                            </td>
                        </tr>
                        </tbody>
                        <tfoot>
                        <tr>
                            <td colspan="12" align="center">
                                <ul class="pagination">
                                    <li>
                                        <a th:href="@{/admin/toCourseList(pageNum=${pageQueryUtils.prePageNum},name=${name})}">上一页</a>
                                    </li>

                                    <li>
                                        <a th:href="@{/admin/toCourseList(pageNum=${pageQueryUtils.pageNum},name=${name})}">[[${pageQueryUtils.pageNum}]]</a>
                                    </li>
                                    <li>
                                        <a th:href="@{/admin/toCourseList(pageNum=${pageQueryUtils.nextPageNum},name=${name})}">下一页</a>
                                    </li>
                                </ul>
                            </td>
                        </tr>

                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>

</div>
</div>
<div th:replace="common/admin_commonElement :: jsFile"></div>
<script type="text/javascript">
    layui.use('layer', function () {
        var layer = layui.layer;
    });

    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
    });

    $("#selectByNameBtn").click(function () {
        // 获取模糊查询的id
        var name = $("#nameInput").val();

        // 获取当前页的页码
        var pageNum = [[${pageQueryUtils.pageNum}]];

        // 带着参数重新跳转到本页面
        window.location.href = projectPath + "/admin/toCourseList?name=" + name + "&pageNum=" + pageNum;
    });

    // 编辑课程信息
    function editCourse(id) {
        window.location.href = projectPath + "/admin/toCourseEdit?id=" + id;
    }

    // 删除课程信息
    function deleteCourse(id) {
        var pageNum = [[${pageQueryUtils.pageNum}]];
        var name = $("#nameInput").val();
        $.ajax({
            //url
            url: projectPath + '/admin/deleteCourse',
            //参数:
            data: {
                id: id
            },
            //请求类型：
            type: 'delete',
            //响应体结果：
            dataType: 'json',
            //成功回调：
            success: function (data) {
                if (data.result == "SUCCESS") {
                    layer.open({
                        title: "删除成功"
                        , content: "删除成功，3秒后自动跳转刷新页面！请耐心等待……"
                        , time: 3000
                        , end: function () {
                            window.location.href = projectPath + "/admin/toCourseList?pageNum=" + pageNum + "&name=" + name;
                        }
                    });
                } else {
                    layer.open({
                        title: "删除失败"
                        , content: "删除失败，可能是服务器故障，请稍后再试……"
                        , time: 3000
                    });
                }
            },
            //失败回调：
            error: function () {
                layer.open({
                    title: "删除失败"
                    , content: "删除失败，可能是服务器故障，请稍后再试……"
                    , time: 3000
                });
            },
        });
    }

    // 添加学生
    $("#addCourse").click(function () {
        window.location.href = projectPath + "/admin/toAddCourse";
    });

</script>
</body>
</html>